package com.example.loginsystem.service.impl;

import com.example.loginsystem.bean.MyUser;
import com.example.loginsystem.bean.Role;
import com.example.loginsystem.dao.MyUserDAO;
import com.example.loginsystem.model.Paging;
import com.example.loginsystem.model.Result;
import com.example.loginsystem.service.MyUserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import java.sql.PreparedStatement;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author Cheung0
 * Created on 2021/11/21
 */
@Component
public class MyUserServiceImpl implements MyUserService {

    @Autowired
    private MyUserDAO myUserDAO;

    @Autowired
    private PasswordEncoder encoder;

    @Override
    public Result<MyUser> add(MyUser myUser) {
        Result result = new Result();
        MyUser getMyUser = myUserDAO.getByUsername(myUser.getUserName());
        if (getMyUser != null) {
            result.setResultFailed("待添加的用户的用户名已存在！");
            return result;
        }
        myUser.setPassword(encoder.encode(myUser.getPassword()));
        if (myUserDAO.addUser(myUser) != 1) {
            result.setResultFailed("添加用户失败！");
            return result;
        }
        int userId = myUserDAO.getUserId(myUser.getUserName());
        int roleId = myUser.getRoles().iterator().next().getId();
        if (myUserDAO.addUserRole(userId, roleId) != 1) {
            result.setResultFailed("分配角色失败！");
            return result;
        }
        result.setResultSuccess("添加成功！", myUser);
        return result;
    }

    @Override
    public Result<MyUser> delete(int id) {
        Result result = new Result();
        if (myUserDAO.deleteUser(id) != 1) {
            result.setResultFailed("删除失败！");
            return result;
        }
        result.setResultSuccess("删除成功！");
        return result;
    }

    @Override
    public Result<MyUser> update(MyUser myUser) {
        Result result = new Result();
        MyUser getMyUser = myUserDAO.getByUsername(myUser.getUserName());
        if (getMyUser == null) {
            result.setResultFailed("该用户不存在！");
            return result;
        }
        if(myUserDAO.updateUser(myUser) != 1) {
            result.setResultFailed("更新用户失败！");
        }
        int userId = myUserDAO.getUserId(myUser.getUserName());
        int roleId = myUser.getRoles().iterator().next().getId();
        if (myUserDAO.updateUserRole(userId, roleId) != 1) {
            result.setResultFailed("更新角色失败！");
            return result;
        }
        result.setResultSuccess("更新成功！", myUser);
        return result;
    }

    @Override
    public Result<MyUser> getByUserName(String userName) {
        MyUser myUser = new MyUser();
        myUser = myUserDAO.getByUsername(userName);
        Result result = new Result();
        result.setResultSuccess("获取成功！", myUser);
        return result;
    }

    @Override
    public Paging<MyUser> getByPage(int current, int size) {
        Page<MyUser> userPage = PageHelper.startPage(current, size).doSelectPage(() -> myUserDAO.getAll());
        Paging<MyUser> userPaging = new Paging<>(userPage);
        return userPaging;
    }

    @Override
    public List<MyUser> getAll() {
        return myUserDAO.getAll();
    }

}
